package org.zachary.creditbusiness.commodityandhouse.service.impl;

import org.zachary.creditbusiness.commodityandhouse.entity.House;
import org.zachary.creditbusiness.commodityandhouse.mapper.HouseMapper;
import org.zachary.creditbusiness.commodityandhouse.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@Service
public class HouseServiceImpl implements HouseService {

    @Autowired
    private HouseMapper houseMapper;

    @Override
    public House getHouseById(Integer id) {
        return houseMapper.selectById(id);
    }

    @Override
    public List<House> getAllHouses() {
        return houseMapper.selectList(null);
    }

    @Override
    public House createHouse(House house) {
        houseMapper.insert(house);
        return house;
    }

    @Override
    public House updateHouse(House house) {
        houseMapper.updateById(house);
        return house;
    }

    @Override
    public void deleteHouse(Integer id) {
        houseMapper.deleteById(id);
    }

    @Override
    public Page<House> getHouses(int page, int size) {
        Page<House> pageObj = new Page<>(page, size);
        return houseMapper.selectPage(pageObj, null);
    }

    @Override
    public Page<House> searchByTitle(String title, int page, int size) {
        Page<House> pageObj = new Page<>(page, size);
        QueryWrapper<House> queryWrapper = new QueryWrapper<House>().like("title", title);
        return houseMapper.selectPage(pageObj, queryWrapper);
    }

    @Override
    public List<House> searchByTitle(String title) {
        return houseMapper.selectList(new QueryWrapper<House>().like("title", title));
    }

    @Override
    public Page<House> filterByDistrict(String district, int page, int size) {
        Page<House> pageObj = new Page<>(page, size);
        QueryWrapper<House> queryWrapper = new QueryWrapper<House>().eq("district", district);
        return houseMapper.selectPage(pageObj, queryWrapper);
    }

    @Override
    public List<House> filterByDistrict(String district) {
        return houseMapper.selectList(new QueryWrapper<House>().eq("district", district));
    }

    @Override
    public Page<House> filterByPriceRange(Double minPrice, Double maxPrice, int page, int size) {
        Page<House> pageObj = new Page<>(page, size);
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        if (minPrice != null) {
            queryWrapper.ge("price", minPrice);
        }
        if (maxPrice != null) {
            queryWrapper.le("price", maxPrice);
        }
        return houseMapper.selectPage(pageObj, queryWrapper);
    }

    @Override
    public List<House> filterByPriceRange(Double minPrice, Double maxPrice) {
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        if (minPrice != null) {
            queryWrapper.ge("price", minPrice);
        }
        if (maxPrice != null) {
            queryWrapper.le("price", maxPrice);
        }
        return houseMapper.selectList(queryWrapper);
    }

    @Override
    public Page<House> filterByRentalType(String rentalType, int page, int size) {
        Page<House> pageObj = new Page<>(page, size);
        QueryWrapper<House> queryWrapper = new QueryWrapper<House>().eq("rental_type", rentalType);
        return houseMapper.selectPage(pageObj, queryWrapper);
    }

    @Override
    public List<House> filterByRentalType(String rentalType) {
        return houseMapper.selectList(new QueryWrapper<House>().eq("rental_type", rentalType));
    }

    @Override
    public Page<House> filterHouses(String title, String district, String rentalType,
                                    Double minPrice, Double maxPrice, int page, int size) {
        Page<House> pageObj = new Page<>(page, size);
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();

        // 标题模糊查询
        if (title != null && !title.trim().isEmpty()) {
            queryWrapper.like("title", title.trim());
        }

        // 地区精确查询
        if (district != null && !district.trim().isEmpty()) {
            queryWrapper.eq("district", district.trim());
        }

        // 租赁类型精确查询
        if (rentalType != null && !rentalType.trim().isEmpty()) {
            queryWrapper.eq("rental_type", rentalType.trim());
        }

        // 价格范围查询
        if (minPrice != null) {
            queryWrapper.ge("price", minPrice);
        }
        if (maxPrice != null) {
            queryWrapper.le("price", maxPrice);
        }

        return houseMapper.selectPage(pageObj, queryWrapper);
    }
}